﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;

public partial class PayTypeTree : System.Web.UI.Page
{
    FinaceTypeBll bll = new FinaceTypeBll();

    protected string CallBack
    {
        get
        {
            return Request.QueryString["CallBack"];
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Response.Expires = -1;
            this.BindTree();

            this.TreeView1.Nodes[0].Selected = true;
            //TreeView1_SelectedNodeChanged(null, null);
        }
    }

    private void BindTree()
    {
        List<FinaceType> lstPayTypes = bll.SelectFinaceTypes(HttpMemberInfo.MemerID);

        TreeNode node = new TreeNode();
        node.Text = "<a href='javascript:void(0);'><input type='hidden' value='-1' /><span>所有分类</span></a>";
        node.Value = "-1";
        this.TreeView1.Nodes.Add(node);
        this.BuidTree(node, lstPayTypes);
    }

    private void BuidTree(TreeNode parentNode, List<FinaceType> soure)
    {
        List<FinaceType> lst = soure.Where(t => t.ParentTypeID == int.Parse(parentNode.Value)).ToList<FinaceType>();

        
        foreach (var model in lst)
        {
            TreeNode node = new TreeNode();
            node.Value = model.TypeID.ToString();

            string strHiddenValue = "<a href='javascript:void(0);'><input type='hidden' value='{0}' /><span>" + model.TypeName + "</span></a>";
            strHiddenValue = String.Format(strHiddenValue, node.Value);
            node.Text = strHiddenValue  ;
            node.SelectAction = TreeNodeSelectAction.Expand; 
            this.BuidTree(node, soure);
            parentNode.ChildNodes.Add(node);
        }

    }

    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        // string script = String.Empty;
        //string script = String.Format("window.parent.{0}('{1}','{2}')", this.CallBack, this.TreeView1.SelectedValue, this.TreeView1.SelectedNode.Text);
        //// string script = "window.parent.SelectTreeNode("+this.TreeView1.SelectedValue+");";

        //ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "@script", script, true);
    }
}
